package com.example.seh.person.post;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.seh.R;
import com.example.seh.square.Post;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

public class MyPostAdapter extends BaseAdapter {

    private Context context;
    private List<Post> lists;
    private int item;
    private String[] pics;

    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    private static DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
    private String outputDate;


    public MyPostAdapter(Context context, List<Post> lists, int item) {
        this.context = context;
        this.lists = lists;
        this.item = item;
    }

    @Override
    public int getCount() {
        if (lists != null) {
            return lists.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (lists != null) {
            return lists.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.title = convertView.findViewById(R.id.post_title);
            viewHolder.time = convertView.findViewById(R.id.post_time);
            viewHolder.time2 = convertView.findViewById(R.id.post_time2);
            viewHolder.reply = convertView.findViewById(R.id.post_reply);
            viewHolder.pic1 = convertView.findViewById(R.id.post_pic1);
            viewHolder.pic2 = convertView.findViewById(R.id.post_pic2);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (lists.get(position).getPicPath() != null) {
            pics = lists.get(position).getPicPath().split(";");
            if (!pics[0].isEmpty()) {
                viewHolder.pic1.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(pics[0])
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .dontAnimate()
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                if (target instanceof ImageView) {
                                    ((ImageView) target).setTag(pics[0]);  // 绑定加载请求与视图
                                }
                                return false;
                            }
                        })
                        .into(viewHolder.pic1);
            } else {
                Glide.with(context)
                        .clear(viewHolder.pic1);  // 清除之前的加载请求，避免复用时显示错误的图片
                viewHolder.pic1.setImageDrawable(null);  // 或者设置默认占位图像

            }

            if (pics.length >= 2 && !pics[1].isEmpty()) {
                viewHolder.pic2.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(pics[1])
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .dontAnimate()
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                if (target instanceof ImageView) {
                                    ((ImageView) target).setTag(pics[1]);  // 绑定加载请求与视图
                                }
                                return false;
                            }
                        })
                        .into(viewHolder.pic2);
            } else {
                Glide.with(context)
                        .clear(viewHolder.pic2);  // 清除之前的加载请求，避免复用时显示错误的图片
                viewHolder.pic2.setImageDrawable(null);  // 或者设置默认占位图像

            }
        } else {
            viewHolder.pic1.setVisibility(View.GONE);
            viewHolder.pic2.setVisibility(View.GONE);
        }


        try {
            Date date = dateFormat.parse(lists.get(position).getCreateTime());
            outputDate = outputFormat.format(date);
            String[] dateParts = outputDate.split("-");

            viewHolder.time.setText(dateParts[0]);
            viewHolder.time2.setText(dateParts[1] + "-" + dateParts[2]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        viewHolder.title.setText(lists.get(position).getTitle());

        int replyNum = lists.get(position).getReply();
        if (replyNum == 0) {
            viewHolder.reply.setText("回复");
        } else {
            viewHolder.reply.setText(lists.get(position).getReply() + "");
        }
        return convertView;
    }

    class ViewHolder {
        TextView title, time, time2, reply;
        ImageView pic1, pic2;
    }
}
